package cn.iktz.mather.know.web.config;

import java.net.InetAddress;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource(value = "classpath:elasticsearch.properties")
public class ElasticConfig {

	@Value("${elasticsearch.index}")
	private String elasticsearch_index;
	@Value("${elasticsearch.type}")
	private String elasticsearch_type;
	@Value("${elasticsearch.port}")
	private int port;
	@Value("${elasticsearch.hostname}")
	private String hostname;

	@Value("${elasticsearch.cluster.name}")
	String clusterName;

	@Bean
	public Client client() {
		try {
			Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName)// 我的集群名
					.put("client.transport.sniff", false).build();

			Client client = TransportClient.builder().settings(settings).build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), port));
			return client;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("创建client错误");
		}
	}

	@Autowired
	private Client client;

	public String getElasticsearch_index() {
		return elasticsearch_index;
	}

	public void setElasticsearch_index(String elasticsearch_index) {
		this.elasticsearch_index = elasticsearch_index;
	}

	public String getElasticsearch_type() {
		return elasticsearch_type;
	}

	public void setElasticsearch_type(String elasticsearch_type) {
		this.elasticsearch_type = elasticsearch_type;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public String getClusterName() {
		return clusterName;
	}

	public void setClusterName(String clusterName) {
		this.clusterName = clusterName;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	@Bean
	public SearchRequestBuilder build() {
		SearchRequestBuilder searchRequestBuilder = client.prepareSearch(elasticsearch_index);
		searchRequestBuilder.setTypes(elasticsearch_type);
		return searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
	}

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	
}